/*#########################################################################
# Amparok by Saggi Mizrahi.                                               #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program; if not, write to the                         #
#   Free Software Foundation, Inc.,                                       #
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         #
##########################################################################*/

Importer.loadQtBinding("qt.core");
Importer.loadQtBinding("qt.xml");
Importer.loadQtBinding("qt.gui");
Importer.loadQtBinding("qt.network");
Importer.loadQtBinding("qt.uitools");

Importer.include("common.js");
Importer.include("httpServer.js");
Importer.include("ampacheAdapter.js");

var conf;

function loadConfig()
{
  listenH = {"local": QHostAddress.LocalHost, "any": QHostAddress.Any};
  conf = {};
  conf.listenTo = listenH[Amarok.Script.readConfig("listen_to", "any")];
  conf.port = parseInt(Amarok.Script.readConfig("port", "5556"), 10);
  conf.acl = eval(Amarok.Script.readConfig("acl", "{'user':'password'}"));
  return conf;
}

function onConfigure()
{
  dlg = createWindow('dlgConfig.ui');
  dlg.cbListenTo.addItem("LocalHost");
  dlg.cbListenTo.addItem("Any");
  if (conf.listenTo == QHostAddress.LocalHost)
  {
    dlg.cbListenTo.setCurrentIndex(0);
  }
  else
  {
    dlg.cbListenTo.setCurrentIndex(1);
  }
  dlg.sbPort.setValue(conf.port);
  for (user in conf.acl)
  {
    dlg.lstUsers.addItem(user);
  }
  dlg.btnAddUser.clicked.connect( function() 
  {
    re = "^[a-zA-Z_]+$";
    text = QInputDialog.getText(dlg, "Add a user:", "Must match " + re, QLineEdit.Normal, "", false);
    
    if (text)
    {
       if (!text.match(re))
       {
	alert("Invalid user name", "sorry");
       }
       else if (conf.acl[text] !== undefined)
       {
	 alert("User allready exists", "sorry");
       }
       else
       {
	conf.acl[text] = "Password";
	dlg.lstUsers.addItem(text);
	alert("User created. Password is \"Password\". Please change it as soon as possible.");
       }
    }
  });
  dlg.btnDeleteUser.clicked.connect( function() 
  {
    i = dlg.lstUsers.item(dlg.lstUsers.currentRow);
    u = i.text();
    delete conf.acl[u];
    dlg.lstUsers.clear();
    for (user in conf.acl)
    {
      dlg.lstUsers.addItem(user);
    }
  });
  dlg.btnSetPassword.clicked.connect( function() 
  {
    i = dlg.lstUsers.item(dlg.lstUsers.currentRow);
    u = i.text();
    text = QInputDialog.getText(dlg, "Change password:", "For user " + u, QLineEdit.Normal, "", false);
    
    if (text)
    {
       if (conf.acl[u] !== undefined)
       {
	 conf.acl[u] = text;
       }
    }
  });
  if(dlg.exec())
  {
    listenH = ["local", "any"];
    Amarok.Script.writeConfig("listen_to", listenH[dlg.cbListenTo.currentIndex]);
    Amarok.Script.writeConfig("port", dlg.sbPort.value.toString());
    Amarok.Script.writeConfig("acl", toJson(conf.acl));
    alert("You need to restart the script for your changes to take place");
  }
  loadConfig();
}

debug("Ampache Adapter", 'hello', 3);

loadConfig();

Amarok.Window.addSettingsMenu( "amparokConfig", "Configure Amparok...", "browser" );
Amarok.Window.SettingsMenu.amparokConfig.triggered.connect( onConfigure ); 

server = new HTTPServer(new QHostAddress(conf.listenTo), conf.port);  
adapter = new AmpacheAdapter(conf.acl);
server.routes.push([adapter.route, function(s, r, r2)
  {adapter._processRequest.call(adapter, s, r, r2);}
  ]);
server.listen();
notify("Amparok", 'Amparok server started...');